package vip.xiaonuo.core.scheduled;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import vip.xiaonuo.biz.modular.apilog.entity.DevApiLog;
import vip.xiaonuo.biz.modular.apilog.mapper.DevApiLogMapper;
import vip.xiaonuo.biz.modular.event.service.BizEventDoorService;
import vip.xiaonuo.biz.modular.event.service.BizEventGoodsLedgerService;
import vip.xiaonuo.biz.modular.event.service.BizEventReagentService;
import vip.xiaonuo.biz.modular.event.service.BizEventService;
import vip.xiaonuo.common.cache.CommonCacheOperator;
import vip.xiaonuo.common.util.WashDataUtil;

import javax.annotation.Resource;

/**
 * @author 墨塵
 * @since 2022/11/30
 */
@Service
@Slf4j
public class EventScheduled {

    @Resource
    private DevApiLogMapper devApiLogMapper;

    @Resource
    private BizEventService bizEventService;

    @Resource
    private BizEventGoodsLedgerService bizEventGoodsLedgerService;

    @Resource
    private BizEventDoorService bizEventDoorService;

    @Resource
    private BizEventReagentService bizEventReagentService;

    @Resource
    private CommonCacheOperator commonCacheOperator;

    /**
     * 处理事件 【间隔1秒】
     */
    @Scheduled(fixedDelay = 1000 * 1)
    public void handleEvent() {
        //从reids里面取
        Object flag = commonCacheOperator.get(WashDataUtil.WASH_KEY);
        if (flag != null) {
            if (Integer.valueOf(flag.toString()) > 0) {
                //获取未处理的接口日志【排除 6 7 9】
                DevApiLog apiLog = devApiLogMapper.getOneRecordForAnalysis();
                if (null != apiLog) {
                    if (apiLog.getReqType() == 1) {
                        //开门
                        bizEventService.handleOpenDoor(apiLog);
                    }
                    if (apiLog.getReqType() == 2) {
                        //关门
                        bizEventService.handleCloseDoor(apiLog);
                    }
                    if (apiLog.getReqType() == 3) {
                        //手动盘库
                        bizEventService.handleForManInv(apiLog);
                    }
                    if (apiLog.getReqType() == 4) {
                        //自动盘库
                        bizEventService.handleForAutoInv(apiLog);
                    }
                    if (apiLog.getReqType() == 5) {
                        //客户端重启
                        bizEventService.handleForRestart(apiLog);
                    }
                    if (apiLog.getReqType() == 8) {
                        //刷卡鉴权
                        bizEventService.handleForAuth(apiLog);
                    }
                    if (apiLog.getReqType() == 10) {
                        //环境事件
                        bizEventService.handleAmbEvent(apiLog);
                    }
                    if (apiLog.getReqType() == 11) {
                        //称重事件
                        bizEventService.handleWeigh(apiLog);
                    }
                    //移除一个
                    commonCacheOperator.put(WashDataUtil.WASH_KEY, Integer.valueOf(commonCacheOperator.get(WashDataUtil.WASH_KEY) == null ? "0" : commonCacheOperator.get(WashDataUtil.WASH_KEY).toString()) - 1);
                }
            }
        }
    }

    /**
     * 处理定期事件 物品借出【间隔5秒】【爆炸吧 boom!!!!!】
     */
    @Scheduled(fixedDelay = 1000 * 5)
    public void handleGoodsOverdue() {
        bizEventGoodsLedgerService.handleGoodsLend();
    }

    /**
     * 处理定期事件 未关门【间隔10秒】
     */
    @Scheduled(fixedDelay = 1000 * 10)
    public void handleDoorNotClose() {
        bizEventDoorService.handleDoorNotClose();
    }

    /**
     * 处理定期事件 过期试剂【间隔1分钟】
     */
    @Scheduled(fixedDelay = 1000 * 60)
    public void handleExpReagent() {
        bizEventReagentService.handleExpReagent();
    }
}
